//
//
//#pragma once
//
//#include <cinatra/render.h>
//
//namespace syllabus {
//    using Dao = dao_t<dbng<mysql>>;
//    using std::string,std::tuple;
//    class SyllabusController {
//    public:
//        SyllabusController() {
////            Dao dao;
////            auto v = dao.query<std::tuple<std::string, std::string>>
////            ("SELECT term_id, name from pp_terms  where term_id in (1, 2,3,4,5,7,8,25,47,58)");
////            for (auto tp : v) {
////                category_map_.emplace(std::get<0>(tp), std::get<1>(tp));
////            }
//
//        }
//
//        ~SyllabusController() {
//            stop_counter_ = true;
//        }
//
//        void home(request& req, response& res) {
//            const auto& params = req.get_aspect_data();
//            const auto& s = params.size()==2 ? params[0] : params[3];
//            const auto& lens = params.size() == 2 ? params[1] : params[4];
//
////            if (total_post_count_ == 0) {
////                Dao dao;
////                auto v = dao.query<std::tuple<int>>("SELECT count(1) from pp_posts  where post_status = 'publish'");
////                if (v.empty()) {
////                    res.set_status_and_content(status_type::not_found);
////                    return;
////                }
////
////                total_post_count_ = std::get<0>(v[0]);
////            }
//
//            auto session = req.get_session().lock();
//            if (session == nullptr || session->get_data<std::string>("user_name").empty()) {
//                auto new_session = res.start_session();
//                new_session->set_max_age(-1);
//            }
//
//            size_t cur_page = atoi(s.data())/10+1;
//            std::string sql;
//            if(session->get_data<int>("user_role")!=0){
//                sql = "SELECT plan.* "
//                      "from school, user ,plan"
//                      "where school.id=user.school_id  "
//                      "AND school.id= plan.school_id"
//                      "AND t1.category<>60 "
//                      "ORDER BY t1.ID DESC LIMIT "+s+","+lens;
//            }else{
//                sql = "SELECT plan.* "
//                      "from school, user ,plan"
//                      "where school.id=user.school_id  "
//                      "AND school.id= plan.school_id"
//                      "AND t1.category<>60 "
//                      "ORDER BY t1.ID DESC LIMIT "+s+","+lens;
//            }
//
//            render_page(sql, req, res, "./www/html/home.html", "all", cur_page, total_post_count_);
//        }
//        static void login_page(request& req, response& res) {
//            render_simple_page(req, res, "www/html/login.html", "login");
//        }
//
//
//
//        static void login(request& req, response& res) {
//            const auto& params = req.get_aspect_data();
//            const auto& user_name = params[0];
//            const auto& password = params[1];
//            string sql = "select ID, user_role from user where user_login='" + user_name +
//                              "' and user_pass=md5('" + password+"')";
//            Dao dao;
//            auto r = dao.query<tuple<string, string>>(sql);
//            if (r.empty()) {
//                res.set_status_and_content(status_type::ok, "user name or password is not right");
//                return;
//            }
//            std::string user_id = std::get<0>(r[0]);
//            std::string user_role = std::get<1>(r[0]);
//            init_session(req, res, user_id, user_role, user_name);
//            res.redirect("/syllabus/plan.html");
//        }
//
//
//    private:
//        void render_page(const std::string& sql, request& req, response& res, const std::string& html_file,
//        std::string categroy, size_t cur_page=0, size_t total=0) {
//            Dao dao;
//            auto v = dao.query<tuple<string>>(sql);
//            if (v.empty()) {
//                res.set_status_and_content(status_type::ok, "");
//                return;
//            }
//
//            const auto& params = get_user_info(req);
//            const auto& login_user_name = params.empty() ? "" : params[0];
//            const auto& user_role = params.empty() ? "" : params[2];
//
//            bool is_admin = (!login_user_name.empty()) && (user_role == "3" || user_role == "6");
//
//            nlohmann::json article_list;
//            for (auto& o : v) {
//                nlohmann::json item;
//                auto& post = std::get<0>(o);
//            }
//
//            if (total == 0) {
//                total = v.size();
//            }
//
//            nlohmann::json result;
//            result["article_list"] = article_list;
//            result["has_login"] = !login_user_name.empty();
//            result["login_user_name"] = login_user_name;
//            result["current_page"] = cur_page;
//            result["total"] = total;
//            result["category"] = categroy;
//            res.add_header("Content-Type", "text/html; charset=utf-8");
//            res.set_status_and_content(status_type::ok, render::render_file(html_file, result));
//        }
//
//        static void render_simple_page(request& req, response& res, const std::string& html_file, const std::string& category="");
//
//        static void init_session(request& req, response& res, const std::string& user_id,
//                          const std::string& user_role, const std::string& user_name) {
//            auto session = req.get_session().lock();
//            if (session == nullptr) {
//                auto new_session = res.start_session();
//                new_session->set_data("user_name", user_name);
//                new_session->set_data("userid", user_id);
//                new_session->set_data("user_role", user_role);
//                new_session->set_max_age(-1);
//            }
//            else {
//                if (session->get_data<std::string>("user_name").empty()) {
//                    session->set_data("user_name", user_name);
//                    session->set_data("userid", user_id);
//                    session->set_data("user_role", user_role);
//                }
//            }
//        }
//
//    private:
//        std::atomic<size_t> total_post_count_ = 0;
//        std::map<std::string, std::string> category_map_;
//
//        bool stop_counter_ = false;
//
//
//    };
//
//    void SyllabusController::render_simple_page(request &req, response &res, const std::string& html_file,
//                                                const std::string &category) {
//        auto login_user_name = get_user_name_from_session(req);
//        nlohmann::json result;
//        result["has_login"] = !login_user_name.empty();
//        result["login_user_name"] = login_user_name;
//        result["category"] = category;
//        res.add_header("Content-Type", "text/html; charset=utf-8");
//        res.set_status_and_content(status_type::ok, render::render_file(html_file, result));
//    }
//}
